package com.finace.money.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v4.app.Fragment;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.finace.money.R;
import com.finace.money.common.Constant;
import com.finace.money.common.util.ACache;
import com.finace.money.common.util.ACacheClearUtil;
import com.finace.money.common.util.StringUtil;
import com.finace.money.common.util.UIUtil;
import com.finace.money.di.component.AppComponent;
import com.finace.money.net.socket.ConnService;
import com.finace.money.ui.fragment.FragmentFactory;

import butterknife.BindView;

public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener{

    @BindView(R.id.rbs_fragment)
    RadioGroup rbsFragment;
    private int mPosition = 0;
    private String mTickId;

    public String getmTickId() {
        return mTickId;
    }

    @Override
    public int setLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void init() {

        mPosition = getIntent().getIntExtra(Constant.main, mPosition);
        changeFragment(mPosition);
        changeRadioButton(mPosition);
        rbsFragment.setOnCheckedChangeListener(this);
        /*requestSdWritePermission(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if(aBoolean){

                }else {
                    UIUtil.showToastSafe("获取权限失败，有可能影响您的使用");
                }
            }
        });*/
    }

    @Override
    public void setupAcitivtyComponent(AppComponent appComponent) {

    }

    @Override
    protected void onNewIntent(Intent intent) {
        setIntent(intent);
        if (null != intent) {
            int position = getIntent().getIntExtra(Constant.main, mPosition);
            mTickId = getIntent().getStringExtra("id");
            changeRadioButton(position);
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedID) {

        switch (checkedID) {
            case R.id.rb_main_home: //首页
                mPosition = FragmentFactory.TAB_HOME;
                break;
            case R.id.rb_main_status:  //行情
                mPosition = FragmentFactory.TAB_STATUS;
                break;
            case R.id.rb_main_have: //持仓

                String login = ACache.get(UIUtil.getContext()).getAsString(Constant.login);
                if(StringUtil.isNullString(login)){
                    startActivity(new Intent(UIUtil.getContext(), LoginActivity.class));
                }else {
                    mPosition = FragmentFactory.TAB_HAVE;
                }
                break;
            case R.id.rb_main_news:  //咨询
                mPosition = FragmentFactory.TAB_NEWS;
                break;
            case R.id.rb_main_mine:  //我的

                String login2 = ACache.get(UIUtil.getContext()).getAsString(Constant.login);
                if(StringUtil.isNullString(login2)){
                    startActivity(new Intent(UIUtil.getContext(), LoginActivity.class));
                }else {
                    mPosition = FragmentFactory.TAB_MINE;
                }
                break;
        }
        changeFragment(mPosition);
    }

    public Fragment changeFragment(int position) {
        this.mPosition = position;
        return FragmentFactory.changeFragment(getSupportFragmentManager(), position, R.id.fl_container);
    }

    public void changeRadioButton(int position) {
        if (null != rbsFragment.getChildAt(position))
            ((RadioButton) rbsFragment.getChildAt(position)).setChecked(true);
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        outState.putInt("position", mPosition);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        mPosition = savedInstanceState.getInt("position");
        changeFragment(mPosition);
        super.onRestoreInstanceState(savedInstanceState);
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (mPosition == FragmentFactory.TAB_STATUS) {
            changeFragment(mPosition).onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        ACacheClearUtil.clear();
        Intent intent = new Intent(this, ConnService.class);
        stopService(intent);
    }
}
